package com.wac.data.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wac.data.entity.User;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

	private static ObjectMapper objectMapper = new ObjectMapper();
	private static SimpleMailMessage emialSender = new SimpleMailMessage();
	//邮箱验证码
	private String code;

	@Resource
	private UserService userService;
	@Value("${spring.mail.username}")
	private String senderName;
	@Resource
	private JavaMailSender javaMailSender;


	// 生成验证码
	private void createEmailCode() {
		this.code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
		System.out.println("生成邮箱验证码:" + this.code);
	}

	// 发送邮件验证码
	@RequestMapping("/code")
	public String sendEmail(String email) {
		createEmailCode();
		System.out.println("发送给:" + email);
		emialSender.setFrom(senderName);//发送方
		emialSender.setTo(email);//送达邮件
		emialSender.setSubject("WaaFun网站注册验证码");//主题
		emialSender.setText("你的验证码是: " + this.code + " 不要告诉别人哦！");//内容
		try {
			javaMailSender.send(emialSender);
		} catch (MailSendException m) {
			System.out.println(m);
			return "failed";
		}
		return "success";
	}

	// 验证用户填写的验证码
	@RequestMapping("/codeAffirm")
	public String isCodeRight(String userCode) {
		if (userCode.equals(this.code)) {
			return "success";
		}
		return "failed";
	}

	@RequestMapping("/isLogin")
	public String isLogin(HttpServletRequest request) {
		String token = new DataListUtil().getCookies(request);
		if (token == null) {
			return "0";
		}
		String userId = userService.isUserLogin(token);
		if (userId != null) {
			return userId;
		}
		return "0";
	}

	@RequestMapping("/loginIn")
	public String loginIn(String email, String password, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (userId != null) {
			response.sendRedirect("/state/user/reLogin");
		}
		String token = userService.loginIn(email, password);
		if (token != null) {
			return token;
		}
		return "0";
	}

	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request) {
		String token = new DataListUtil().getCookies(request);
		if (userService.loginOut(token)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/fieldAffirm")
	public String fiedldAffirm(String field, String content) {
		if (userService.isFieldLegal(field, content)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/register")
	public String register(@RequestBody User user) {
		if (userService.register(user)) {
			return "1";
		}
		return "0";
	}

	//1是详细信息 0是简易信息
	@RequestMapping("/user")
	public String getInfo(String id, int type) throws JsonProcessingException {
		if (type == 1) {
			return objectMapper.writeValueAsString(userService.getUserFullInfo(id));
		}
		if (type == 0) {
			return objectMapper.writeValueAsString(userService.getUserSimpleInfo(id));
		}
		return "error";
	}

	//签到相关
	@RequestMapping("/isCheck")
	public String isCheck(HttpServletRequest request) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (userService.isUserCheckIn(userId)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/check")
	public String check(HttpServletRequest request) {
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		if (userService.check(userId)) {
			return "1";
		}
		return "0";
	}

	//朋友列表
	@RequestMapping("/friend")
	public String getFriend(int page, String id) throws JsonProcessingException {
		return objectMapper.writeValueAsString(userService.getUserFriendList(page, id));
	}

	//图片上传 1是头像 2背景图
	@RequestMapping("/update/file")
	public String updateUser(HttpServletRequest request, @RequestPart User user, @RequestPart List<MultipartFile> file, int where) {
		System.out.println("传头像");
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		user.setId(userId);
		if (userService.updateUser(user, file, where)) {
			return "1";
		}
		return "0";
	}

	@RequestMapping("/update/text")
	public String updateUser(HttpServletRequest request, @RequestPart User user) {
		System.out.println("传文本");
		String userId = userService.isUserLogin(new DataListUtil().getCookies(request));
		user.setId(userId);
		if (userService.updateUser(user, null, 0)) {
			return "1";
		}
		return "0";
	}
}

